package com.cskaoyan.market.component;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.ObjectMetadata;
import com.aliyun.oss.model.PutObjectResult;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.cskaoyan.market.aliyun.AliyunProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.InputStream;

@Service
public class AliyunService implements CloudService{

    @Autowired
    AliyunProperties aliyunProperties;

    @Override
    public String oss(File file, String fileName) {
        String accessKeyId = aliyunProperties.getAccessKeyId();
        String accessKeySecret = aliyunProperties.getAccessKeySecret();
        String endPoint = aliyunProperties.getOss().getEndPoint();
        String bucketName = aliyunProperties.getOss().getBucket();

        ObjectMetadata objectMetadata = new ObjectMetadata();
        objectMetadata.setContentType("image/jpg");
        //创建一个oss客户端
        OSS ossClient = new OSSClientBuilder().build(endPoint, accessKeyId, accessKeySecret);
        //设置文件名
        objectMetadata.setContentDisposition("inline;filename=" + fileName);
        //获取硬盘上面的文件
        //使用客户端来进行文件上传操作
        PutObjectResult putObjectResult = ossClient.putObject(bucketName, fileName, file,objectMetadata);
        System.out.println(putObjectResult);

        ossClient.shutdown();

//        https://{bucketName}.{endPoint}/filename
        String path = "https://" + bucketName + "." + endPoint + "/" + fileName;
        return path;
    }

    @Override
    public String oss(InputStream inputStream, String fileName) {
        String accessKeyId = aliyunProperties.getAccessKeyId();
        String accessKeySecret = aliyunProperties.getAccessKeySecret();
        String endPoint = aliyunProperties.getOss().getEndPoint();
        String bucketName = aliyunProperties.getOss().getBucket();

        ObjectMetadata objectMetadata = new ObjectMetadata();
        objectMetadata.setContentType("image/jpg");
        //创建一个oss客户端
        OSS ossClient = new OSSClientBuilder().build(endPoint, accessKeyId, accessKeySecret);
        //设置文件名
        objectMetadata.setContentDisposition("inline;filename=" + fileName);
        //获取硬盘上面的文件
        //使用客户端来进行文件上传操作
        PutObjectResult putObjectResult = ossClient.putObject(bucketName, fileName, inputStream,objectMetadata);
        System.out.println(putObjectResult);

        ossClient.shutdown();

//        https://{bucketName}.{endPoint}/filename
        String path = "https://" + bucketName + "." + endPoint + "/" + fileName;
        return path;
    }

    @Override
    public Integer sms(String phoneNumber, String code) {
        String accessKeyId = aliyunProperties.getAccessKeyId();
        String accessKeySecret = aliyunProperties.getAccessKeySecret();
        //设定的签名名称
        String signName = aliyunProperties.getSms().getSignName();
        //使用的模板名称：如下方，显示的固定字符即为模板里面的内容
        String templateCode = aliyunProperties.getSms().getTemplateCode();
        //需要接收验证码的手机

        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", phoneNumber);
        request.putQueryParameter("SignName", signName);
        request.putQueryParameter("TemplateCode", templateCode);
        request.putQueryParameter("TemplateParam", "{\"code\":\"" + code + "\"}");
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
        } catch (Exception e) {
            e.printStackTrace();
            return 404;
        }
        return 200;
    }
}
